<html>
<head><meta charset="utf-8"><title>docs · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/docs.html">docs</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="205388939"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/docs/near/205388939" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Leon Matthes <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/docs.html#205388939">(Jul 29 2020 at 17:40)</a>:</h4>
<p>Hi everyone, I found out that a Boxed Slice (specifically Box&lt;[u8]&gt;) actually stores two usize values (the pointer + the size) on the stack, and only stores the u8-data on the heap. I needed to make sure Box::leak() didn't leave anything other than the actual array data on the heap. I was passing ownership to a C library and needed to know.<br>
Due to the syntax Box&lt;[u8]&gt;, I thought at first, a Box is just a single pointer, pointing to a slice on the heap, which is a (sort off) struct, containing a pointer to the array + the length as usize.<br>
Is it documented somewhere that Box&lt;[u8]&gt; is layed out as a pointer + size on the stack? I was only able to find out by using std::mem::size_of::&lt;Box&lt;[u8]&gt;&gt;().<br>
I think this is valuable knowledge for anyone using the FFI.</p>



<a name="205389045"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/docs/near/205389045" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/docs.html#205389045">(Jul 29 2020 at 17:41)</a>:</h4>
<p>slice is defined as just elements in memory without a size</p>



<a name="205389088"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/docs/near/205389088" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/docs.html#205389088">(Jul 29 2020 at 17:41)</a>:</h4>
<p>So there are never other things on the heap with <code>Box&lt;[u8]&gt;</code>, i.e. the size is always stored as part of the pointer</p>



<a name="205389214"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/docs/near/205389214" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/docs.html#205389214">(Jul 29 2020 at 17:42)</a>:</h4>
<blockquote>
<p>Is it documented somewhere that Box&lt;[u8]&gt; is layed out as a pointer + size on the stack?</p>
</blockquote>
<p>It should</p>



<a name="205389224"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/docs/near/205389224" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/docs.html#205389224">(Jul 29 2020 at 17:42)</a>:</h4>
<p>let me search <span aria-label="sweat smile" class="emoji emoji-1f605" role="img" title="sweat smile">:sweat_smile:</span></p>



<a name="205389272"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/docs/near/205389272" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/docs.html#205389272">(Jul 29 2020 at 17:43)</a>:</h4>
<p>The abi of pointers to dst's (including <code>Box&lt;[T]&gt;</code>) is undefined.</p>



<a name="205389434"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/docs/near/205389434" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/docs.html#205389434">(Jul 29 2020 at 17:44)</a>:</h4>
<p>it is defined that <code>[T]</code> never contains data about itself</p>



<a name="205389485"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/docs/near/205389485" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/docs.html#205389485">(Jul 29 2020 at 17:44)</a>:</h4>
<p>Also you should prefer <code>Box::into_raw</code> over <code>Box::leak</code>. The later is for when you really want to leak the memory, while the former is for for example ffi cases. Make sure to use <code>Box::from_raw</code> to deallocate the memory though. You are not allowed to use <code>free</code> to deallocate memory allocated by rust, as rust may use a different allocator than C using <code>#[global_allocator]</code>.</p>



<a name="205389586"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/docs/near/205389586" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/docs.html#205389586">(Jul 29 2020 at 17:45)</a>:</h4>
<blockquote>
<p>I was passing ownership to a C library and needed to know.</p>
</blockquote>
<p>Then you must use <code>malloc</code> or <code>std::alloc::System.alloc</code>.</p>



<a name="205389859"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/docs/near/205389859" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Leon Matthes <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/docs.html#205389859">(Jul 29 2020 at 17:47)</a>:</h4>
<p><span class="user-mention silent" data-user-id="133247">bjorn3</span> <a href="#narrow/stream/122651-general/topic/docs/near/205389272">said</a>:</p>
<blockquote>
<p>The abi of pointers to dst's (including <code>Box&lt;[T]&gt;</code>) is undefined.</p>
</blockquote>
<p>Does that mean the behavior of passing the pointer + size of a Box&lt;[T]&gt; to a C function would lead to undefined behavior? What would be the correct way of passing a variable length buffer to a C function?</p>



<a name="205390099"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/docs/near/205390099" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/docs.html#205390099">(Jul 29 2020 at 17:49)</a>:</h4>
<blockquote>
<p>Does that mean the behavior of passing the pointer + size of a Box&lt;[T]&gt; to a C function would lead to undefined behavior? </p>
</blockquote>
<p>It is unspecified. It depends on the exact abi rustc chooses and what the C function expects.</p>



<a name="205390106"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/docs/near/205390106" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Leon Matthes <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/docs.html#205390106">(Jul 29 2020 at 17:49)</a>:</h4>
<p><span class="user-mention silent" data-user-id="216206">lcnr</span> <a href="#narrow/stream/122651-general/topic/docs/near/205389434">said</a>:</p>
<blockquote>
<p>it is defined that <code>[T]</code> never contains data about itself</p>
</blockquote>
<p>Ah, okay, where is that? I was unable to find it myself...<br>
<a href="https://doc.rust-lang.org/std/primitive.slice.html">https://doc.rust-lang.org/std/primitive.slice.html</a> is somewhat vague on this and defines a slice as a <code>view int a contiguous sequence</code> which to me suggests the slice itself is the view (so the pointer + size), which isn't the case.</p>



<a name="205390110"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/docs/near/205390110" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/docs.html#205390110">(Jul 29 2020 at 17:49)</a>:</h4>
<p>Sounds like you might want <code>Vec</code> instead</p>



<a name="205390115"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/docs/near/205390115" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/docs.html#205390115">(Jul 29 2020 at 17:49)</a>:</h4>
<p><span class="user-mention silent" data-user-id="216206">lcnr</span> <a href="#narrow/stream/122651-general/topic/docs/near/205389434">said</a>:</p>
<blockquote>
<p>it is defined that <code>[T]</code> never contains data about itself</p>
</blockquote>
<p>hmm, this info isn't as public as I thought. It isn't really possible for this to not be the case, considering that we have O(1) subslicing and the ability to use ptr offset to navigate slices...</p>



<a name="205390329"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/docs/near/205390329" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/docs.html#205390329">(Jul 29 2020 at 17:51)</a>:</h4>
<p><span class="user-mention silent" data-user-id="133247">bjorn3</span> <a href="#narrow/stream/122651-general/topic/docs/near/205390099">said</a>:</p>
<blockquote>
<blockquote>
<p>Does that mean the behavior of passing the pointer + size of a Box&lt;[T]&gt; to a C function would lead to undefined behavior? </p>
</blockquote>
<p>It is unspecified. It depends on the exact abi rustc chooses and what the C function expects.</p>
</blockquote>
<p>Wait, it is defined to pass the size and ptr of a <code>Box</code> to a C function, isn't it? It is undefined to drop said pointer without using <code>Box::from_raw</code> though</p>



<a name="205390345"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/docs/near/205390345" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Leon Matthes <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/docs.html#205390345">(Jul 29 2020 at 17:51)</a>:</h4>
<p><span class="user-mention silent" data-user-id="211727">Jonas Schievink</span> <a href="#narrow/stream/122651-general/topic/docs/near/205390110">said</a>:</p>
<blockquote>
<p>Sounds like you might want <code>Vec</code> instead</p>
</blockquote>
<p>That's actually what I'm using now, but then I have to store the capacity somewhere else when deallocating later.<br>
Is the ABI for Vec defined when passing to an FFI function?</p>



<a name="205390435"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/docs/near/205390435" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Leon Matthes <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/docs.html#205390435">(Jul 29 2020 at 17:52)</a>:</h4>
<p>But my journey from Box&lt;[T]&gt; still left me curious <span aria-label="laughing" class="emoji emoji-1f606" role="img" title="laughing">:laughing:</span></p>



<a name="205390452"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/docs/near/205390452" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/docs.html#205390452">(Jul 29 2020 at 17:52)</a>:</h4>
<p><span class="user-mention silent" data-user-id="216206">lcnr</span> <a href="#narrow/stream/122651-general/topic/docs/near/205390329">said</a>:</p>
<blockquote>
<p><span class="user-mention silent" data-user-id="133247">bjorn3</span> <a href="#narrow/stream/122651-general/topic/docs/near/205390099">said</a>:</p>
<blockquote>
<blockquote>
<p>Does that mean the behavior of passing the pointer + size of a Box&lt;[T]&gt; to a C function would lead to undefined behavior? </p>
</blockquote>
<p>It is unspecified. It depends on the exact abi rustc chooses and what the C function expects.</p>
</blockquote>
<p>Wait, it is defined to pass the size and ptr of a <code>Box</code> to a C function, isn't it? It is undefined to drop said pointer without using <code>Box::from_raw</code> though</p>
</blockquote>
<p>If you explicitly separate the ptr and size then it works. If you pass <code>Box&lt;[T]&gt;</code> directly then it isn't allowed.</p>



<a name="205390568"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/docs/near/205390568" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Leon Matthes <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/docs.html#205390568">(Jul 29 2020 at 17:53)</a>:</h4>
<p><span class="user-mention silent" data-user-id="133247">bjorn3</span> <a href="#narrow/stream/122651-general/topic/docs/near/205390452">said</a>:</p>
<blockquote>
<p><span class="user-mention silent" data-user-id="216206">lcnr</span> <a href="#narrow/stream/122651-general/topic/docs/near/205390329">said</a>:</p>
<blockquote>
<p><span class="user-mention silent" data-user-id="133247">bjorn3</span> <a href="#narrow/stream/122651-general/topic/docs/near/205390099">said</a>:</p>
<blockquote>
<blockquote>
<p>Does that mean the behavior of passing the pointer + size of a Box&lt;[T]&gt; to a C function would lead to undefined behavior? </p>
</blockquote>
<p>It is unspecified. It depends on the exact abi rustc chooses and what the C function expects.</p>
</blockquote>
<p>Wait, it is defined to pass the size and ptr of a <code>Box</code> to a C function, isn't it? It is undefined to drop said pointer without using <code>Box::from_raw</code> though</p>
</blockquote>
<p>If you explicitly separate the ptr and size then it works. If you pass <code>Box&lt;[T]&gt;</code> directly then it isn't allowed.</p>
</blockquote>
<p>Aaaaah, okay, I was passing the two separately, so it's all good. You had me quite worried there for a second <span aria-label="sweat smile" class="emoji emoji-1f605" role="img" title="sweat smile">:sweat_smile:</span>  .</p>



<a name="205391035"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/docs/near/205391035" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Leon Matthes <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/docs.html#205391035">(Jul 29 2020 at 17:57)</a>:</h4>
<p><span class="user-mention silent" data-user-id="216206">lcnr</span> <a href="#narrow/stream/122651-general/topic/docs/near/205389434">said</a>:</p>
<blockquote>
<p>it is defined that <code>[T]</code> never contains data about itself</p>
</blockquote>
<p>hmm, this info isn't as public as I thought. It isn't really possible for this to not be the case, considering that we have O(1) subslicing and the ability to use ptr offset to navigate slices...</p>
<div class="codehilite"><pre><span></span><code>Where do you think would be the correct place to put such information? It makes a lot of sense that this is the case, but I was still searching to find confirmation of this for about an hour, and it didn&#39;t seem explicitely stated anywhere.

It&#39;s one of those cases where having it wrong would lead to a tiny memory leak that would be extremely hard to find.
I figured I could write a few sentences to create a PR to expand the docs on this myself and wanted to know where to start?
</code></pre></div>



<a name="205391234"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/docs/near/205391234" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/docs.html#205391234">(Jul 29 2020 at 17:58)</a>:</h4>
<p>Probably in the docs of <code>slice</code> itself.</p>



<a name="205391253"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/docs/near/205391253" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/docs.html#205391253">(Jul 29 2020 at 17:59)</a>:</h4>
<p><a href="https://doc.rust-lang.org/std/primitive.slice.html">https://doc.rust-lang.org/std/primitive.slice.html</a></p>



<a name="205391521"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/docs/near/205391521" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Leon Matthes <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/docs.html#205391521">(Jul 29 2020 at 18:00)</a>:</h4>
<p><span class="user-mention silent" data-user-id="216206">lcnr</span> <a href="#narrow/stream/122651-general/topic/docs/near/205391234">said</a>:</p>
<blockquote>
<p>Probably in the docs of <code>slice</code> itself.</p>
</blockquote>
<p>Thanks, I'll try to get a PR up on this in the next while.<br>
Thanks <span class="user-mention" data-user-id="216206">@lcnr</span> &amp; <span class="user-mention" data-user-id="133247">@bjorn3</span> for your help.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>